package com.yupi.yupicturebackend.controller;


import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.yupicturebackend.annotation.AuthCheck;
import com.yupi.yupicturebackend.common.BaseResponse;
import com.yupi.yupicturebackend.common.ResultUtils;
import com.yupi.yupicturebackend.exception.ErrorCode;
import com.yupi.yupicturebackend.exception.ThrowUtils;
import com.yupi.yupicturebackend.model.dto.TagCategory.TagCategoryAddRequest;
import com.yupi.yupicturebackend.model.entity.TagCategory;
import com.yupi.yupicturebackend.model.entity.User;
import com.yupi.yupicturebackend.model.vo.picture.PictureTagCategory;
import com.yupi.yupicturebackend.service.TagCategoryService;
import com.yupi.yupicturebackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/tagCategory")
@Api(tags = "标签分类接口")
public class TagCategoryController {


    @Resource
    private TagCategoryService tagCategoryService;
    @Resource
    private UserService userService;

    /**
     * 获取标签分类
     * @return
     */
    @ApiOperation(value = "获取标签分类")
    @GetMapping("/tag_category")
    public BaseResponse<PictureTagCategory> listPictureTadCategory(){
        PictureTagCategory pictureTagCategory = tagCategoryService.getPictureTagCategory();
        return ResultUtils.success(pictureTagCategory);
    }

    /**
     * 管理员增添标签分类
     */
    @ApiOperation(value = "管理员添加标签分类")
    @GetMapping("/add_admin")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> addTagCategoryAdmin(@RequestBody TagCategoryAddRequest addRequest, HttpServletRequest request){
        ThrowUtils.throwIf(addRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        TagCategory tagCategory = new TagCategory();
        tagCategory.setName(addRequest.getName());
        tagCategory.setUserId(loginUser.getId());
        tagCategory.setLevel(addRequest.getLevel());
        //公共的标签分类
        tagCategory.setPublic_no(0);
        return ResultUtils.success(tagCategoryService.save(tagCategory));
    }

    @ApiOperation(value = "用户添加标签分类")
    @GetMapping("/add_user")
    public BaseResponse<Boolean> addTagCategory(@RequestBody TagCategoryAddRequest addRequest, HttpServletRequest request){
        ThrowUtils.throwIf(addRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        TagCategory tagCategory = new TagCategory();
        tagCategory.setName(addRequest.getName());
        tagCategory.setUserId(loginUser.getId());
        tagCategory.setLevel(addRequest.getLevel());
        //非公共的标签分类
        tagCategory.setPublic_no(1);
        return ResultUtils.success(tagCategoryService.save(tagCategory));
    }



}
